#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python_learning 
@File ：装饰器模式.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2024/3/1 23:55 
"""

"""
装饰器模式（Decorator Pattern）
模式解释：
装饰器模式允许向现有对象添加新功能，同时又不改变其结构。
"""

from abc import ABC, abstractmethod


class Component(ABC):
	@abstractmethod
	def operation(self):
		pass


class ConcreteComponent(Component):
	def operation(self):
		return "ConcreteComponent operation."


class Decorator(Component):
	def __init__(self, component):
		self._component = component

	def operation(self):
		return f"Decorator operation:\n{self._component.operation()}"


class ConcreteDecoratorA(Decorator):
	def added_operation(self):
		return "ConcreteDecoratorA additional operation."

	def operation(self):
		return f"{super().operation()}\n{self.added_operation()}"


class ConcreteDecoratorB(Decorator):
	def added_operation(self):
		return "ConcreteDecoratorB additional operation."

	def operation(self):
		return f"{super().operation()}\n{self.added_operation()}"


component = ConcreteComponent()
decorator_a = ConcreteDecoratorA(component)
decorator_b = ConcreteDecoratorB(decorator_a)

print(decorator_b.operation())
